package com.example.seh.person;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.seh.R;
import com.example.seh.constant.Constant;
import com.example.seh.home.substitution.DeliverDTO;
import com.example.seh.util.UserMsg;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class PasswordExchangeActivity extends AppCompatActivity {
    private TextView account;
    private EditText oldPassword, newPassword;
    private Button submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_exchange);
        getViews();
        account.setText(UserMsg.userDTO.getPhone());
        setListeners();
    }

    private void setListeners() {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(oldPassword.getText().toString()) || newPassword.getText().toString().trim().length() == 0) {
                    Toast.makeText(getApplicationContext(), "请填写完整", Toast.LENGTH_SHORT).show();
                } else {
                    if (oldPassword.getText().toString().equals(newPassword.getText().toString())) {
                        Toast.makeText(getApplicationContext(), "两次输入的密码不能相同", Toast.LENGTH_SHORT).show();
                    } else {
                        Log.e("pas","old:" + oldPassword.getText().toString() + " new:" + newPassword.getText().toString());
                        OkHttpClient okHttpClient = new OkHttpClient();
                        JSONObject object = new JSONObject();
                        Gson gson = new Gson();
                        try {
                            object.put("userId", UserMsg.userDTO.getUserId());
                            object.put("password", newPassword.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        MediaType type = MediaType.parse("application/json;charset=utf-8");
                        RequestBody body = RequestBody.create(type, object.toString());
                        Request request = new Request.Builder()
                                .url(Constant.UPDATEUSER)
                                .post(body)
                                .build();
                        Call call = okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                Log.e("okhttp", "获取数据失败");
                                e.printStackTrace();
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String result = response.body().string();
                                Log.e("pas",result);
                                if ("更新信息成功".equals(result)) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                    finish();
                                } else {
                                    Toast.makeText(getApplicationContext(), "修改失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
            }
        });
    }

    private void getViews() {
        account = findViewById(R.id.password_exchange_account);
        oldPassword = findViewById(R.id.password_exchange_password);
        newPassword = findViewById(R.id.password_exchange_password_new);
        submit = findViewById(R.id.password_exchange_submit);
    }
}